<template>
    <v-card>
        <v-flex xs12 sm10>
            <v-tree url="/item/category/of/parent"
                    :isEdit="isEdit"
                    @handleAdd="handleAdd"
                    @handleEdit="handleEdit"
                    @handleDelete="handleDelete"
                    @handleClick="handleClick">
            </v-tree>
        </v-flex>
    </v-card>
</template>

<script>
    export default {
        name: "category",
        data() {
            return {
                isEdit: true
            }
        },
        methods: {
            handleAdd(node) {
                //获取输入框的值赋值给name  ***js中的promise
                this.$message.prompt("请输入分类名", "").then((input) => {
                    this.$http.post("/item/category/add", {
                            isParent: node.isParent,
                            name: input,
                            parentId: node.parentId,
                            sort: node.sort,
                        }
                    ).then(resp => node.id = resp).catch(() => {
                        this.$message.error("服务器异常,新增失败")
                    });
                });
            },

            handleEdit(id, name) {
                this.$http.put("/item/category/update", {
                    id: id,
                    name: name
                }).then(() => {
                    this.$message.info("修改成功")
                }).catch(() => {
                    this.$message.error("服务器异常 修改失败")
                })
            },

            handleDelete(id) {
                this.$http.delete("/item/category/delete/" + id);
            },

            handleClick(node) {
                console.log(node)
            }
        }
    };
</script>

<style scoped>

</style>
